<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,initial-scale=1.0,width=device-width"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>大奔农庄</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/aui.css"/>
    <link rel="stylesheet" type="text/css" href="../../css/style.css"/>    
    <link rel="stylesheet" href="../../css/swiper.min.css">
    <link rel="stylesheet" href="../../font/iconfont.css">
</head>
<body>
	<header class="aui-bar aui-bar-nav top-nav" id="title">
	<a class="aui-pull-left aui-btn"></a> 
		<div class="aui-title title">购物车</div>
	</header>
	<div class="aui-content padding-45" style="padding-bottom:95px;">
	    <div id="wudongtai_box">
			<img src="../../image/wudongtai.png" id="wudongtai_img" />
			<p id="wudongtai_txt">购物车是空的</p>
		</div>	
	    <ul class="aui-list aui-media-list" id="cartBox">
	    <!--
		    <li class="aui-list-item gwc_tb2 db_cart_li_box"> 
			 	<div class="aui-media-list-item-inner">
			       	<div class="aui-list-item-media db_aui-list-item-media">
			           	<img src="http://daben.demo.dx623.com/Public/Uploads/Picture/2017-07-18/596dc6138dd1d.jpg">
			       	</div>
			        <div class="aui-list-item-inner db_cart_li_box_desc">
			        	<div class="aui-list-item-text">
			            	<div class="aui-list-item-title db_aui-list-item-title">小大米</div>
			            </div>
			            <div class="">
			            	<div class="db_car_li_box_desc_text">单价：100</div>
			            	<div class="db_car_li_box_desc_text" id="goods_xiaoji{$vo.gid}">小计：100</div>
						</div>
						<div class="tb1_td5 aui-pull-right db_aui-pull-right">
							<input id=jian{$vo.gid} type="button" value="-" onclick="doCarJian({$vo.gid});"/>
							<input id="number{$vo.gid}" data-id="{$vo.gid}" data-price="{$vo.price}" type="text" readonly value="1"  />
							<input id="jia{$vo.gid}" type="button" value="+" onclick="doCarJia({$vo.gid});"/>
						</div>
			        </div>
				</div>
			    <div class="aui-clearfix"></div>
			</li>
		-->	
    	</ul>
    </div>
	<!--底部-->
	<footer class="aui-bar aui-bar-tab spinfo-foot none" id="cartfooter">
		<input class="aui-checkbox aui-pull-left aui-margin-l-15 allselect none" type="checkbox" id="checkAll">
		<span class="aui-pull-left aui-font-size-14 none" style="line-height:2.2rem !important;">全选</span>
		<div class="aui-col-xs-4 aui-bg-success aui-pull-right" style="line-height:2.2rem;" onclick="openTijiao();">结算</div>
		<span class="aui-font-size-14 aui-pull-right car_zongji_box">
			合计：<span class="aui-text-danger" id="cd_carSum" class="tot">0.00</span>
		</span>	
	</footer>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/common.js"></script>
<script>
var db_key = 'user',db_user,allCartSum;
apiready = function(){
	getuser();
	//监听购物车数据库改变事件
    api.addEventListener({
        name: 'goods_tocar'
    }, function(ret, err) {
        loadCart();
    });
	loadCart();//加载购物车列表
}
//获取用户储存在本地的数据
function getuser() {
    db_user = $api.getStorage(db_key);
}
//加载内容
function loadCart() {
	api.showProgress();//显示加载进度框
	var useParam = {
    	rtk : RSapi.myrtk,
        mytoken : db_user.mytoken,
    };
	api.ajax({
		url: RSapi.base+RSapi.CartList,
		method: 'post',
		cache: false,
		timeout: 30,
		dataType: 'json',
		data: { values: useParam },
	}, function(ret, err){
		if(err){
			//api.toast({msg: JSON.stringify(err)});
			api.toast({msg: err.msg});
		}else {
			if (ret.status == 1) { 
				//成功了
				if(ret.carSum){
					allCartSum = ret.carSum;
					$api.html($api.byId('cd_carSum'),'￥'+allCartSum);//显示已经添加购物车的商品总价
				}	
				//显示无动态图片
				$api.addCls($api.byId('wudongtai_box'), 'none');//显示无动态框
				$api.html($api.byId("cartBox"), ' ');//清空当前由前面数据
				$api.append($api.byId("cartBox"),ret.info);//展示从服务端获取的新数据
				$api.removeCls($api.byId('cartfooter'), 'none');//显示下面的总计和结算功能块
				api.parseTapmode();
				eval('status_'+status+'=true;');
			}else if(ret.status == 2){
				//没有内容
				$api.html($api.byId("cartBox"), ' ');//清空当前由前面数据
				//隐藏结算按钮
				$api.addCls($api.byId('cartfooter'), 'none');//显示无动态框
				//显示无动态图片
				$api.removeCls($api.byId('wudongtai_box'), 'none');//显示无动态框
				//实现无动态提示
				$api.html($api.byId('wudongtai_txt'), ret.info);//显示提示内容
			}else {
				//失败了
				api.toast({msg: ret.info});
			}
		}
	});
	api.hideProgress();
}
//购买数量操作
function doCarJia(id){	
	BuyNumbObj = $api.byId('number'+id);//获取当前商品数量行对象
	xiaojiojb = $api.byId('goods_xiaoji'+id);//获取小计对象
	
	OldNumb = Number($api.val(BuyNumbObj)); //获取当前数量
	gid = BuyNumbObj.dataset.id//获取商品id
	price = Number(BuyNumbObj.dataset.price);//获取当前商品价格
	
	NewNumb = OldNumb+1;//当前数量+1
	$api.val(BuyNumbObj,NewNumb);//改变页面上当前产品数量
	
	xiaoji = Number(NewNumb * price);//计算商品当前小计
	$api.html(xiaojiojb, '小计：￥'+xiaoji);//小计赋值
	
	allCartSum = allCartSum+price;//计算新的购物车总计数据
	$api.html($api.byId('cd_carSum'),'￥'+allCartSum);//显示已经添加购物车的商品总价
	
	adToCar(gid,NewNumb);//操作服务端	
}
function doCarJian(id){	
	BuyNumbObj = $api.byId('number'+id);//获取当前商品数量行对象
	xiaojiojb = $api.byId('goods_xiaoji'+id);//获取小计对象
	
	OldNumb = Number($api.val(BuyNumbObj)); //获取当前数量
	gid = BuyNumbObj.dataset.id//获取商品id
	price = Number(BuyNumbObj.dataset.price);//获取当前商品价格
	
	NewNumb = OldNumb-1;//当前数量+1	
	$api.val(BuyNumbObj,NewNumb);//改变页面上当前产品数量
	//如果数量小于1移除改行
	if(NewNumb < 1){
		$api.remove($api.byId('goodsid'+gid));
	}	
	
	xiaoji = Number(NewNumb * price);//计算商品当前小计
	$api.html(xiaojiojb,'小计：￥'+xiaoji);//小计赋值
	
	allCartSum = allCartSum-price;//计算当前购物车总计
	$api.html($api.byId('cd_carSum'),'￥'+allCartSum);//显示已经添加购物车的商品总价
	
	//如果总计=0，显示无内容
	if(allCartSum <= 0){
		$api.addCls($api.byId('cartfooter'), 'none');
		$api.html($api.byId('wudongtai_txt'), '您的购物车里面什么也没有！');//显示提示内容
		$api.removeCls($api.byId('wudongtai_box'), 'none');//显示无动态框
	}
	
	adToCar(gid,NewNumb);//操作服务端	
}
//加入购物车
function adToCar(gid,buynum){
	if (db_user.islogin) {
		var useParam = {
	    	rtk : RSapi.myrtk,
	        mytoken : db_user.mytoken,
	        goods_num : buynum,
	        gid : gid,
	    };
	    api.ajax({
	        url: RSapi.base+RSapi.Tocar,
	        method: 'post',
	        cache: false,
	        timeout: 30,
	        dataType: 'json',
	        data: { values: useParam },
	    }, function(ret, err){
	        if(err){
	            //alert(JSON.stringify(err));
	            api.toast({msg: JSON.stringify(err)});
	        }else {
	            if (ret.status == 1) {
	                //成功了
//	                api.alert({msg: '加入购物车成功！'});
					if(buynum < 1){
						loadCart();//重新加载购物车列表
					}
	            }else {
	                //失败了
	                api.toast({msg: ret.info});
	            }
	        }
	    });
	    return false;
    }else {
        api.alert({ msg: '请先登录! ' });
    }
}
//提交订单页面
function openTijiao(){
	api.openWin({
        name: 'win_ordertijiao',
        url: '../win/ordertijiao_win.html',
    });
}
</script>
</html>